From cf4d67985262c0adf7770d1a9258b5f451538971 Mon Sep 17 00:00:00 2001 From: tsteven4 <13596209+tsteven4@users.noreply.github.com> Date: Mon, 28 Aug 2023 17:21:28 -0600 Subject: [PATCH] fix memory leak in mkshort from d7dfed6, 1/23/2005. (#1162) --- defs.h | 4 ++-- garmin.cc | 16 ++++++---------- mkshort.cc | 13 ++++++------- 3 files changed, 14 insertions(+), 19 deletions(-) diff --git a/defs.h b/defs.h index ac1f85489..e02425281 100644 --- a/defs.h +++ b/defs.h @@ -854,8 +854,8 @@ using ff_readposn = Waypoint* (*)(posn_status*); struct mkshort_handle_imp; // forward declare, definition in mkshort.cc using short_handle = mkshort_handle_imp*; -char* mkshort(short_handle, const char*, bool); -QString mkshort(short_handle, const QString&); +QByteArray mkshort(short_handle, const char*, bool); +QString mkshort(short_handle, const QString&); short_handle mkshort_new_handle(); QString mkshort_from_wpt(short_handle h, const Waypoint* wpt); void mkshort_del_handle(short_handle* h); diff --git a/garmin.cc b/garmin.cc index 97ccdc385..8bf801052 100644 --- a/garmin.cc +++ b/garmin.cc @@ -888,20 +888,16 @@ waypoint_prepare() * mkshort will do collision detection and namespace * cleaning */ - char* ident = mkshort(mkshort_handle, - global_opts.synthesize_shortnames ? - str_from_unicode(src).constData() : - str_from_unicode(wpt->shortname).constData(), - false); + QByteArray ident = mkshort(mkshort_handle, + global_opts.synthesize_shortnames ? + str_from_unicode(src).constData() : + str_from_unicode(wpt->shortname).constData(), + false); /* Should not be a strcpy as 'ident' isn't really a C string, * but rather a garmin "fixed length" buffer that's padded * to the end with spaces. So this is NOT (strlen+1). */ - write_char_string(tx_waylist[i]->ident, ident, sizeof(tx_waylist[i]->ident)); - - if (global_opts.synthesize_shortnames) { - xfree(ident); - } + write_char_string(tx_waylist[i]->ident, ident.constData(), sizeof(tx_waylist[i]->ident)); // If we were explicitly given a comment from GPX, use that. // This logic really is horrible and needs to be untangled. diff --git a/mkshort.cc b/mkshort.cc index d8945ecb9..ec78bffff 100644 --- a/mkshort.cc +++ b/mkshort.cc @@ -353,7 +353,7 @@ setshort_mustuniq(short_handle h, int i) hdl->must_uniq = i; } -char* +QByteArray mkshort(short_handle h, const char* istring, bool is_utf8) { char* ostring; @@ -556,18 +556,17 @@ mkshort(short_handle h, const char* istring, bool is_utf8) } if (hdl->must_uniq) { - return mkshort_add_to_list(hdl, ostring); + ostring = mkshort_add_to_list(hdl, ostring); } - return ostring; + QByteArray rval(ostring); + xfree(ostring); + return rval; } QString mkshort(short_handle h, const QString& istring) { - char* t = mkshort(h, CSTR(istring), true); - QString r(t); - xfree(t); - return r; + return mkshort(h, CSTR(istring), true); } /* -- 2.30.2